﻿
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.TransferStation;

namespace BCData.TMS.TransferStation
{
    public class TransferStationData : ITransferStationData
    {
        private readonly IDatabaseContext databaseContext;
        private readonly string QUERY = @"SELECT
		    TransferStationId,
		    TransferStationName,
		    CreateTime,
		    EditorId,
		    EditTime,
		    Lng,
		    Lat,
		    ProvinceId,
		    ProvinceName,
		    CityId,
		    CityName,
		    CountyId,
		    CountyName,
		    Address,
		    CompanyId,
		    CompanyName,
		    IsEnable,
		    Remark
        FROM
            transferstation";



        #region 构造函数
        public TransferStationData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新仓库中转站纪录
        /// </summary>
        /// <param name="transferStationEntity"></param>
        /// <returns></returns>
        public async Task<TransferStationEntity> InsertAsync(TransferStationEntity entity)
        {
            return await this.databaseContext.Master.InsertAsync(entity);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改仓库中转站记录
        /// </summary>
        /// <param name="transferStationEntity"></param>
        /// <returns></returns>
        public async Task<TransferStationEntity> UpdateAsync(TransferStationEntity entity)
        {
            return await this.databaseContext.Master.UpdateAsync(entity);
        }
        #endregion 

        #region Delete
        /// <summary>
        /// 删除仓库中转站记录
        /// </summary>
        /// <param name="transferStationId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long transferStationId)
        {
            string sql = "DELETE FROM transferstation WHERE TransferStationId = ?transferStationId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TransferStationId", transferStationId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取仓库中转站记录
        /// </summary>
        /// <param name="transferStationId"></param>
        /// <returns></returns>
        public async Task<TransferStationEntity> GetEntityAsync(long transferStationId)
        {
            string sql = QUERY + " WHERE  TransferStationId = ?transferStationId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TransferStationId", transferStationId, DbType.Int64);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<TransferStationEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询仓库中转站记录
        public async Task<(IEnumerable<TransferStationEntity>, int)> GetListAsync(int pageIndex, int pageSize)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();


            string sql = string.Format(@"{0} {1} ; SELECT COUNT(1) FROM  transferstation  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize
                  );

            return await this.databaseContext.QueryMultipleAsync<TransferStationEntity, int>(sql, parameters);
        }
        #endregion

    }
} 


